package com.qpf.mycms.api.factory;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qpf.mycms.api.abstracts.BaseRequest;
import com.qpf.mycms.api.pojo.PageResult;
import com.qpf.mycms.api.util.HttpServletUtil;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class PageFactory {
    private final static int DEFAULT_PAGE_SIZE = 10;
    private final static int DEFAULT_PAGE_NO = 1;
    private final static String PARAM__PAGE_SIZE = "pageSize";
    private final static String PARAM__PAGE_NO = "pageNo";
    public static <T> Page<T> defaultPage() {
        int pageNo = DEFAULT_PAGE_NO;
        int pageSize = DEFAULT_PAGE_SIZE;
        HttpServletRequest request = HttpServletUtil.getRequest();
        String pageSizeParam = request.getParameter(PARAM__PAGE_SIZE);
        if (!ObjectUtils.isEmpty(pageSizeParam)) {
            pageSize = Integer.parseInt(pageSizeParam);
        }
        String pageNoParam = request.getParameter(PARAM__PAGE_NO);
        if (!ObjectUtils.isEmpty(pageNoParam)) {
            pageNo = Integer.parseInt(pageNoParam);
        }
        return new Page<>(pageNo, pageSize);
    }
    public static <T> Page<T> defaultPage(BaseRequest request) {
        int pageNo = DEFAULT_PAGE_NO;
        int pageSize = DEFAULT_PAGE_SIZE;
        if (!ObjectUtils.isEmpty(request)) {
            pageNo = request.getPageNo() == null ? DEFAULT_PAGE_NO : request.getPageNo();
            pageSize = request.getPageSize() == null ? DEFAULT_PAGE_SIZE : request.getPageSize();
        }
        return new Page<>(pageNo, pageSize);
    }
}
